package com.demo.config;

import org.springframework.amqp.core.Binding;
import org.springframework.amqp.core.Binding.DestinationType;
import org.springframework.amqp.core.DirectExchange;
import org.springframework.amqp.core.Queue;
import org.springframework.amqp.rabbit.connection.ConnectionFactory;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Profile;

/**
 * 消息队列配置文件
 * 
 * @author Kevin
 *
 */
@Configuration
@Profile({"direct_queue_profiles", "config_profiles"})
public class MqConfig {

    @Autowired
    private RabbitTemplate rabbitTemplate;

    /** 直连交换机名称 */
    public final static String DIRECT_EX = "DIRECT_EXCHANGE";

    @Bean
    public DirectExchange directExchange() {
        return new DirectExchange(DIRECT_EX);
    }

    @Bean
    public Queue directQueue() {
        return new Queue("directQueue", true, false ,false , null);
    }

    @Bean
    public Binding directBinding() {
        return new Binding("directQueue", DestinationType.QUEUE, DIRECT_EX, "directQueue", null);
    }

    @Bean
    public RabbitTemplate rabbitTemplate(ConnectionFactory connectionFactory) {
        RabbitTemplate rabbitTemplate = new RabbitTemplate(connectionFactory);
        rabbitTemplate.setMandatory(true);
        rabbitTemplate.setChannelTransacted(true);
        return rabbitTemplate;
    }
}
